#! /user/bin/python3
# coding-utf-8
# @Time = 2024/12/5 11:01
# @Author:Poolaris

from selenium import webdriver
# from webdriver_manager.chrome import ChromeDriverManager
# from webdriver_manager.core.driver_cache import DriverCacheManager
from common.tools import get_project_path, sep


class DriverConfig:
    def driver_config(self):
        """"
        浏览器驱动
        :return:
        """
        options = webdriver.ChromeOptions()
        # 设置浏览器窗口大小
        options.add_argument('--window-size=1920,1080')
        # 隐藏浏览器受到自动测试软件控制的提示
        options.add_experimental_option("excludeSwitches", ["enable-automation"])
        # 解决selenium无法访问https网站的问题
        options.add_argument("--ignore-certificate-errors")
        # 允许忽略localhost上的TLS/SSL错误
        options.add_argument("--allow-insecure-localhost")
        # 设置为无痕模式
        # options.add_argument("--incognito")
        # 设置为无头模式，（后台运行自动化测试）
        # options.add_argument("--headless")

        # 解决卡顿
        # 禁用gpu硬件加速
        options.add_argument("--disable-gpu")
        # 禁用沙箱
        options.add_argument("--no-sandbox")
        # 禁用 Chrome 的 /dev/shm 临时文件系统  使用
        options.add_argument("--disable-dev-shm-usage")
        # options.add_argument("--disable-blink-features=AutomationControlled")
        # driver = webdriver.Chrome(
        #     executable_path=r"E:\Poo\auto\trading_test\chromedriver\chromedriver-win64\chromedriver.exe")
        driver = webdriver.Chrome(executable_path=get_project_path() + sep(
            ["chromedriver", "chromedriver-win64", "chromedriver.exe"], add_sep_before=True), options=options)
        # driver = webdriver.Chrome(ChromeDriverManager(url="https://registry.npmmirror.com/binary.html?path=chromedriver",
        #                                               latest_release_url="https://registry.npmmirror.com/-/binary/chromedriver/LATEST_RELEASE").install(),
        #                           options=options)
        # 删除所有cookies
        driver.delete_all_cookies()
        return driver
